package com.china08.yxyapi.api.home.index.webgroup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.service.sws.EdbWebGroupService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;
import com.squareup.pollexor.ThumborUrlBuilder;
import com.squareup.pollexor.ThumborUrlBuilder.ImageFormat;

@CrossOrigin
@RestController
public class HomeIndexWebGroupList2Controller extends YxyApiBaseController {
    @Autowired
    EdbWebGroupService edbWebGroupService;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/home/index/webgroup/list2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        RespModel respModel = new RespModel();

        Integer bureauLevel = reqModel.bureauLevel;
        String areaIdProvince = reqModel.areaIdProvince;
        String areaIdCity = reqModel.areaIdCity;
        String areaIdDistrict = reqModel.areaIdDistrict;

        List<Pojo> schoolList = new ArrayList<>();

        // 总条数
        Integer allSize = edbWebGroupService.allSizeHomeSchoolKongJianDongTai(bureauLevel, areaIdProvince, areaIdCity, areaIdDistrict);
        allSize = allSize % 5 == 0 ? allSize / 5 : allSize / 5 + 1;
        allSize = allSize > 10 ? 10 : allSize;

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        // 模拟分页，每次查询20条数据，获取所有数据
        for (int i = 1; i <= allSize; i++) {
            Map<String, Object> map = edbWebGroupService.HomeSchoolKongJianDongTaiList(bureauLevel, areaIdProvince, areaIdCity, areaIdDistrict, i);
            if (map != null) {
                list = (List<Map<String, Object>>) map.get("list");
            }

            for (Map<String, Object> m : list) {
                Pojo pojo = new Pojo();
                pojo.schoolNick = m.get("school_name") + "";
                pojo.schoolWebUrl = "http://" + m.get("web_url") + "";
                Object object = m.get("latest_article");

                if (object != null) {
                    pojo.articleCreateDate = DateToolUtils.format((Date) object, "MM-dd");
                } else {

                }

                //
                String schoolId = m.get("school_id") + "";
                if (StringUtils.isNotBlank(schoolId)) {
                    SwsArticle swsArticle = swsArticleRepository.findTop1ByOwnerSchoolIdAndAuditStatusOrderByPublishDateDesc(schoolId, 1);
                    if (swsArticle != null) {
                        pojo.articleId = swsArticle.getId();
                        pojo.articleName = swsArticle.getTitle();
                        pojo.menu1 = swsArticle.getSection1Id();
                        pojo.menu2 = swsArticle.getSection2Id();
                    }
                }
                String schoolBadge = m.get("school_badge") + "";
                if (StringUtils.isBlank(schoolBadge) || "null".equals(schoolBadge)) {
                    schoolBadge = "https://yxyosc.91yunxiao.com/yxyosc/2017/10/16/ca1ab5a0786881b2a231554bb3890925.png";
                }
                if (!schoolBadge.startsWith("http")) {
                    schoolBadge = "https://yxyosc.91yunxiao.com/yxyosc/" + schoolBadge;
                }

                try {
                    // 默认图片不做图片截取处理
                    boolean b1 = schoolBadge.equals("https://yxyosc.91yunxiao.com/yxyosc/2017/10/16/ca1ab5a0786881b2a231554bb3890925.png");

                    // 是否是gif图片
                    boolean b2 = schoolBadge.endsWith("gif") || schoolBadge.endsWith("GIF");

                    //
                    if (!b1 && !b2) {
                        ThumborUrlBuilder tub = ThumborUtil.getThumborInstance().buildImage(schoolBadge);
                        tub.resize(60, 60);
                        tub.smart();
                        tub.trim();
                        tub.filter(ThumborUrlBuilder.quality(100));
                        if (schoolBadge.endsWith("jpg") || schoolBadge.endsWith("jpeg") || schoolBadge.endsWith("JPG") || schoolBadge.endsWith("JPEG")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.JPEG));
                        } else if (schoolBadge.endsWith("PNG") || schoolBadge.endsWith("png")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                        } else if (schoolBadge.endsWith("bmp") || schoolBadge.endsWith("BMP")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.PNG));
                        } else if (schoolBadge.endsWith("gif") || schoolBadge.endsWith("GIF")) {
                            tub.filter(ThumborUrlBuilder.format(ImageFormat.GIF));
                        }
                        schoolBadge = tub.toUrl();
                    }
                } catch (Exception e) {
                }

                pojo.schoolBadge = schoolBadge;
                schoolList.add(pojo);
            }
        }
        respModel.schoolList = schoolList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public Integer bureauLevel;
        public String areaIdProvince;
        public String areaIdCity;
        public String areaIdDistrict;
    }

    public static class Pojo {
        public String schoolBadge;
        public String schoolNick;
        public String articleId;
        public String articleName;
        public String articleCreateDate;
        public String schoolWebUrl;
        public String menu1;
        public String menu2;
    }

    public static class RespModel extends YxyApiRespModel {
        public List<Pojo> schoolList;
    }
}
